package pers.cz.controller;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import pers.cz.commons.datasource.DbKeyHolder;
import pers.cz.commons.datasource.JefSqlSessionTemplate;
import pers.cz.excel.PoiUtils;
import pers.cz.mapper.ImsJsonSoap0Mapper;
import pers.cz.mapper.PostgirlMapper;
import pers.cz.model.ImsJsonSoap0;
import pers.cz.model.Postgirl;
import pers.cz.model.Sequence;
import pers.cz.model.TestCase;
import pers.cz.pub.BeanHelper;
import pers.cz.pub.CpublicFunc;
import pers.cz.pub.SequenceEnum;
import pers.cz.selenium.component.ButtonComponent;
import pers.cz.selenium.component.ContextComponent;
import pers.cz.selenium.component.TextBoxComponent;
import pers.cz.selenium.core.SeleniumChromeFactory;
import pers.cz.service.impl.TestService;
import pers.cz.spring.SpringContextUtil;
import pers.cz.testNg.Cache;
import pers.cz.testNg.TestNg2;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.testng.TestNG;
import pers.cz.thread.ThreadUtils;

import java.util.*;

/**
 * @program: PostGirl-panent
 * @description: TestController
 * @author: Cheng Zhi
 * @create: 2021-02-28 08:59
 **/
@RestController
@RequestMapping("/api/test")
public class TestController {

    //@Autowired
    //SeleniumChromeFactory seleniumChromeFactory;

    @Autowired
    BeanHelper beanHelper;

    @Autowired
    TestService testService;

    @GetMapping("/test1")
    public String Hello() {
        return "hello";
    }

    @GetMapping("/test2")
    public ResponseEntity<byte[]> exportToExcel() {

        TestCase test = new TestCase();

        List<TestCase> testList = new ArrayList<>();
        testList.add(test);

        return PoiUtils.exportToExcel(testList);
    }

    @PostMapping("/test3")
    public void excelToEntity(MultipartFile file) {
/*        TestNg2 testNg = new TestNg2();
        testNg.initFile(file);*/
        Map<String,MultipartFile> cacheMap = new HashMap<String,MultipartFile>();
        cacheMap.put("test",file);
        Cache cache = Cache.getCache();
        cache.setMap(cacheMap);


        TestNG testNG = new TestNG();
        //testNG.setTestClasses(new Class[]{TestNg.class});
        Class[] classes = {TestNg2.class};
        testNG.setTestClasses(classes);
        testNG.run();
    }

    @PostMapping("/test4")
    public void test4() throws InterruptedException {

        for (int i=0; i<10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    CpublicFunc cpublicFunc = new CpublicFunc(beanHelper);
                    Long test_sequence = cpublicFunc.getSoNbr(SequenceEnum.TEST_SEQUENCE);
                    System.out.println(test_sequence);
                }
            });
            thread.start();
        }

    }

    @PostMapping("/test5")
    @Transactional
    public void test5() {



        testService.insertDev();
        testService.insertMaster();

        System.out.println("插入成功");

        if (true) {
            throw new RuntimeException("tt");
        }
    }

    @PostMapping("/test6")
    public Sequence test6(@RequestBody Map map) {

        String sequenceName = (String) map.get("name");
        Sequence select = testService.select(sequenceName);
        return select;
    }
}
